home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CBASE102.ARJ / CBKEYSRC.C < prev    next >
Text File  |  1991-09-23  |  4KB  |  178 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)cbkeysrc.c    1.5 - 91/09/23" */
  5.  
  6. #include <ansi.h>
  7.  
  8. /* ansi headers */
  9. #include <errno.h>
  10. #ifdef AC_STDDEF
  11. #include <stddef.h>
  12. #endif
  13. #ifdef AC_STDLIB
  14. #include <stdlib.h>
  15. #endif
  16. #ifdef AC_STRING
  17. #include <string.h>
  18. #endif
  19.  
  20. /* non-ansi headers */
  21. #include <bool.h>
  22.  
  23. /* library headers */
  24. #include <blkio.h>
  25. #include <btree.h>
  26. #include <lseq.h>
  27.  
  28. /* local headers */
  29. #include "cbase_.h"
  30.  
  31. /*man---------------------------------------------------------------------------
  32. NAME
  33.      cbkeysrch - search cbase key
  34.  
  35. SYNOPSIS
  36.      #include <cbase.h>
  37.  
  38.      int cbkeysrch(cbp, field, buf)
  39.      cbase_t *cbp;
  40.      int field;
  41.      const void *buf;
  42.  
  43. DESCRIPTION
  44.      The cbkeysrch function searches a key in cbase cbp.  field is the
  45.      field to be searched.  buf points to the key for which to be
  46.      searched.  If found, the cursor for that key is positioned to the
  47.      found key.  If it is not found, the cursor for that key is
  48.      positioned to the next higher key, which may be null.  The record
  49.      cursor is set to the record associated with the key marked by the
  50.      key cursor.  Other key cursors are not affected.
  51.  
  52.      cbkeysrch will fail if one or more of the following is true:
  53.  
  54.      [EINVAL]       cbp is not a valid cbase pointer.
  55.      [EINVAL]       field is not a valid field number for
  56.                     cbase cbp.
  57.      [EINVAL]       buf is the NULL pointer.
  58.      [CBELOCK]      cbp is not read locked.
  59.      [CBENKEY]      field is not a key.
  60.      [CBENOPEN]     cbp is not open.
  61.  
  62. SEE ALSO
  63.      cbkcursor, cbkeyfirst, cbkeylast, cbkeynext, cbkeyprev.
  64.  
  65. DIAGNOSTICS
  66.      Upon successful completion, a value of 1 is returned if the key
  67.      was found or a value of 0 if it was not.  Otherwise, a value of
  68.      -1 is returned, and errno set to indicate the error.
  69.  
  70. ------------------------------------------------------------------------------*/
  71. #ifdef AC_PROTO
  72. int cbkeysrch(cbase_t *cbp, int field, const void *buf)
  73. #else
  74. int cbkeysrch(cbp, field, buf)
  75. cbase_t *cbp;
  76. int field;
  77. const void *buf;
  78. #endif
  79. {
  80.     void *    buf2    = NULL;
  81.     cbrpos_t cbrpos    = NIL;
  82.     lspos_t    lspos    = NIL;
  83.     bool    found    = FALSE;
  84.  
  85.     /* validate arguments */
  86.     if (!cb_valid(cbp) || buf == NULL) {
  87.         errno = EINVAL;
  88.         return -1;
  89.     }
  90.  
  91.     /* check if not open */
  92.     if (!(cbp->flags & CBOPEN)) {
  93.         errno = CBENOPEN;
  94.         return -1;
  95.     }
  96.  
  97.     /* validate arguments */
  98.     if (field < 0 || field >= cbp->fldc) {
  99.         errno = EINVAL;
  100.         return -1;
  101.     }
  102.  
  103.     /* check if field is a key */
  104.     if (!(cbp->fldv[field].flags & CB_FKEY)) {
  105.         errno = CBENKEY;
  106.         return -1;
  107.     }
  108.  
  109.     /* check if not read locked */
  110.     if (!(cbp->flags & CBRDLCK)) {
  111.         errno = CBELOCK;
  112.         return -1;
  113.     }
  114.  
  115.     /* construct (key, record position) pair */
  116.     if (btkeysize(cbp->btpv[field]) != (cbp->fldv[field].len + sizeof(cbrpos_t))) {
  117.         CBEPRINT;
  118.         errno = CBEPANIC;
  119.         return -1;
  120.     }
  121.     buf2 = calloc((size_t)1, cbp->fldv[field].len + sizeof(cbrpos_t));
  122.     if (buf2 == NULL) {
  123.         CBEPRINT;
  124.         errno = ENOMEM;
  125.         return -1;
  126.     }
  127.     cbrpos = NIL;
  128.     memcpy(buf2, buf, cbp->fldv[field].len);
  129.     memcpy((char *)buf2 + cbp->fldv[field].len, &cbrpos, sizeof(cbrpos_t));
  130.  
  131.     /* search for key */
  132.     if (btsearch(cbp->btpv[field], buf2) == -1) {
  133.         CBEPRINT;
  134.         free(buf2);
  135.         return -1;
  136.     }
  137.  
  138.     /* check if cursor is null */
  139.     if (btcursor(cbp->btpv[field]) == NULL) {
  140.         free(buf2);
  141.         if (lssetcur(cbp->lsp, NULL) == -1) {
  142.             CBEPRINT;
  143.             return -1;
  144.         }
  145.         return 0;
  146.     }
  147.  
  148.     /* get record position */
  149.     if (btgetk(cbp->btpv[field], buf2) == -1) {
  150.         CBEPRINT;
  151.         free(buf2);
  152.         return -1;
  153.     }
  154.     memcpy(&cbrpos, (char *)buf2 + cbp->fldv[field].len, sizeof(cbrpos));
  155.  
  156.     /* check if key found or not */
  157.     if ((*cbcmpv[cbp->fldv[field].type])(buf, buf2, cbp->fldv[field].len) == 0) {
  158.         found = TRUE;
  159.     } else {
  160.         found = FALSE;
  161.     }
  162.     free(buf2);
  163.     buf2 = NULL;
  164.  
  165.     /* set record cursor */
  166.     lspos = cbrpos;
  167.     if (lspos == NIL) {
  168.         CBEPRINT;
  169.         errno = CBEPANIC;
  170.     }
  171.     if (lssetcur(cbp->lsp, &lspos) == -1) {
  172.         CBEPRINT;
  173.         return -1;
  174.     }
  175.  
  176.     return found ? 1 : 0;
  177. }
  178.